package appx.craft.droid.ui;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import appx.craft.droid.R;
import appx.craft.droid.bean.MessageBean;
import appx.craft.droid.uc.FragmentMessages;
import appx.craft.droid.utils.Const;
import appx.craft.droid.utils.Log;

public class HomeScreen extends FragmentActivity implements OnClickListener {

	public final String TAG = HomeScreen.class.getCanonicalName();
	
	static ArrayList<MessageBean> arrMessagesList;
	MessagesPagerAdapter mAdapter;
	ViewPager mMessageViewPager;
	TextView textView_Index;
	TextView textView_title;
	TextView textView_subtitle;
	Button btn_close,btn_view;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.debug(TAG, "[ onCreate ] ");
		Const.CONTEXT = HomeScreen.this;
		setContentView(R.layout.activity_home_screen);
		
		arrMessagesList = new ArrayList<MessageBean>();
		MessageBean mObecj$MessageBean;
		for(int o=1;o<7;o++){
			mObecj$MessageBean = new MessageBean();
			mObecj$MessageBean.setmMessage$Index(String.valueOf(o));
			mObecj$MessageBean.setmMessage$title(new StringBuilder().append("Title ").append(o).toString());
			mObecj$MessageBean.setmMessage$title(new StringBuilder().append("Sub title ").append(o).toString());
			mObecj$MessageBean.setmMessage$content(new StringBuilder().append("Test Message ").append(o).toString());
			arrMessagesList.add(mObecj$MessageBean);
		}
		
		mAdapter = new MessagesPagerAdapter(getSupportFragmentManager(),arrMessagesList.size());
		mMessageViewPager = (ViewPager)findViewById(R.id.viewPager_messages);
		mMessageViewPager.setAdapter(mAdapter);
		
		mMessageViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				Log.debug(TAG,"[ onPageSelected ] "+ position);
				setMessageIndex(String.valueOf(position+1));
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				Log.debug(TAG,"[ onPageScrolled ] ");
			}
			
			@Override
			public void onPageScrollStateChanged(int position) {
				Log.debug(TAG,"[ onPageScrollStateChanged ] " + position);
			}
		});
		
		textView_Index = (TextView)findViewById(R.id.textView_Index);
		setMessageIndex(String.valueOf(mMessageViewPager.getCurrentItem()+1));
		textView_title = (TextView)findViewById(R.id.textView_title);
		textView_subtitle = (TextView)findViewById(R.id.textView_subtitle);
		
		
		btn_close = (Button)findViewById(R.id.btn_close);
		btn_close.setOnClickListener(this);
		btn_view = (Button)findViewById(R.id.btn_message_view);
		btn_view.setOnClickListener(this);
		
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.debug(TAG, "[ onStart ] ");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.debug(TAG, "[ onStop ] ");
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.debug(TAG, "[ onPause ] ");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.debug(TAG, "[ onResume ] ");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.debug(TAG, "[ onDestroy ] ");
	}
	
	
  private class MessagesPagerAdapter extends FragmentStatePagerAdapter {
	  
    private final int mSize;
	public MessagesPagerAdapter(FragmentManager fm,int size) {
		super(fm);
		 mSize = size;
	}

	@Override
	public Fragment getItem(int position) {
		return  FragmentMessages.newInstance(position);
	}

	@Override
	public int getCount() {
		 return mSize;
	}
    @Override
    public void destroyItem(View container, int position, Object object) {
    	super.destroyItem(container, position, object);
    }      
          
  }
	
  public ArrayList<MessageBean> takeListofMessages(){
	  if(arrMessagesList != null){
			return arrMessagesList;
		}
	  return null;
	}
  
  public void setMessageInMessageList(final int position,MessageBean resetObject){
	  if(arrMessagesList != null){
		  arrMessagesList.remove(position);
		  arrMessagesList.add(position, resetObject);
		  runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				  mAdapter.notifyDataSetChanged();
				  mMessageViewPager.setCurrentItem(position+1);
			}
		});
	  }
  }
  
  /**
   * To Chnage Title of Message Box 
   * @param str$Message
   */
  public void setMessageTitle(String str$Message){
	  
	  if(textView_title != null){
		  
	  }
	  
  }
  
  /**
   * To Chnage sub title of Message Box
   * @param str$Message
   */
  public void setMessageSubTitle(String str$Message){
	  if(textView_subtitle != null){
		  
	  }
  }
  
  public void setMessageIndex(String indexPage){
	  
	  if(textView_Index != null){
		  
		  /**   set message index position **/
		final StringBuilder mBuilder = new StringBuilder();
		mBuilder.append(indexPage);
		mBuilder.append(" of ");
		mBuilder.append(arrMessagesList.size());	 
		runOnUiThread(new Runnable() {
				@Override
				public void run() {
					 textView_Index.setText(mBuilder.toString());
				}
			});
		  
	  }
  }

	@Override
	public void onClick(View v) {
		int viewID = v.getId();
		if(viewID == R.id.btn_close){
			finish();
		}else if(viewID== R.id.btn_message_view){
		}
	
	}
  
}
